package com.zrgj.ctrl;



import com.zrgj.entity.KEY;
import com.zrgj.entity.User;
import com.zrgj.services.LoginServiceImpl;
import com.zrgj.vo.JsonResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
public class LoginController {

    @Resource
    private LoginServiceImpl loginService;

    @PostMapping("/login")
    public JsonResult login(@RequestBody User user, HttpSession session){

        JsonResult jr=new JsonResult(0,"登录成功！");

        try {
            User user1=loginService.login(user);
            if(user1!=null) {
                session.setAttribute(KEY.SESSION_USER, user1);
                jr.setCode(0);
                jr.setMsg("登录成功！");
            }else{
                jr.setCode(1);
                jr.setMsg("登录失败！");
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            jr.setCode(2);
            jr.setMsg("服务器发生错误！");
        }
        return jr;
    }
}
